// 获取鼠标位置类
let mousePos = {
	element:null
}
mousePos.init = function (element) {
	this.element = element
	let mouse = {x:0,y:0,isPressed: false};
	element.addEventListener("mousedown", function(e){
		mouse.isPressed = true;
    let x = 0;
    let y = 0;
	    if (event.pageX || event.pageY){
	        x = event.pageX;
	        y = event.pageY;
	    } else{
	        x = event.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
	        y = event.clientY + document.body.scrollTop + document.documentElement.scrollTop;
	    }
	    x -= element.offsetLeft;
	    y -= element.offsetTop;
	    mouse.x = x;
	    mouse.y = y;
	}, false);

	element.addEventListener("mousemove", function(e){
		if(mouse.isPressed){
      let x = null;
      let y = null;
		    if (event.pageX || event.pageY){
		      x = event.pageX;
		      y = event.pageY;
		    } else{
		      x = event.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
		      y = event.clientY + document.body.scrollTop + document.documentElement.scrollTop;
		    }
		    x -= element.offsetLeft;
		    y -= element.offsetTop;
		   	mouse.x = x;
		    mouse.y = y;
		}
	}, false);

	element.addEventListener("mouseup", function(e){
		mouse.isPressed = false;
    let x = null;
    let y = null;
	    if (event.pageX || event.pageY){
	      x = event.pageX;
	      y = event.pageY;
	    } else{
	      x = event.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
	      y = event.clientY + document.body.scrollTop + document.documentElement.scrollTop;
	    }
	    x -= element.offsetLeft;
	    y -= element.offsetTop;
	    mouse.x = x;
	    mouse.y = y;
	}, false);
	return mouse;
}

export default mousePos
